package cn.cloud9.designpattern.dp08flyweight.ticket;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @projectName: Java
 * @author: 戴知舟
 * @date: 2022年05月24日 13:41
 * @version: 1.0
 */
public class TicketFactory {

    private static Map<String,ITicket> ticketPool = new ConcurrentHashMap<>();

    public static ITicket queryTicket(String from, String to) {
        String key = from + " -> " + to;
        if (ticketPool.containsKey(key)) {
            System.out.println("此票存在，从票池中获取");
            ITicket ticket = ticketPool.get(key);
            return ticket;
        }

        System.out.println("此票不存在，创建后获取");
        TrainTicket trainTicket = new TrainTicket(from, to);
        ticketPool.put(key, trainTicket);
        return trainTicket;
    }

}
